package org.test.client.ui.util.run;

import java.awt.Window;
import java.util.Vector;

import org.test.client.ui.component.window.PopupWindow;

public class FadeWindowRun implements Runnable{
	//暂时没有用
	public static Vector<Window> fadingList = new Vector<Window>();
	
	private Window w = null;
	private boolean dispose = true;
	
	private boolean outFlag = true;
	
    private static final int ANIMATION_FRAMES=50;
    private static final int ANIMATION_INTERVAL=10;
    
    private int frames = ANIMATION_FRAMES;
    
    private int frameIndex = 0;
    
    private boolean canRun = true;
    
	public FadeWindowRun(Window w){
		this(w,true);
	}
	
	
	public FadeWindowRun(Window w, boolean dispose) {
		this(w,dispose,true);
	}

	public FadeWindowRun(Window w, boolean dispose,boolean outFlag) {
		this.w = w;
		this.dispose = dispose;
		this.outFlag = outFlag;
		if(fadingList.contains(w)){
			canRun = false;
		}else{
			fadingList.add(w);
		}
	}

	@Override
	public void run() {
//		if(!canRun){
//			return;
//		}
//		synchronized (w) {
			try {
				if (!outFlag) {
					if (!w.isVisible()) {
						w.setVisible(true);
					}
					com.sun.awt.AWTUtilities.setWindowOpacity(w, 0);
				}
				for (; frameIndex < frames; frameIndex++) {

					float alphaPercent = 1;
					if (outFlag) {
						alphaPercent = (float) (frames - frameIndex - 1)
								/ (float) frames;
					} else {
						alphaPercent = (float) (frameIndex + 1)
								/ (float) frames;
					}
					com.sun.awt.AWTUtilities.setWindowOpacity(w, alphaPercent);
					Thread.sleep(ANIMATION_INTERVAL);
				}
				if (outFlag) {
					if (dispose) {
						w.dispose();
					} else {
						w.setVisible(false);
					}
				}
				fadingList.remove(w);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
//		}
	}


	public boolean isDispose() {
		return dispose;
	}


	public void setDispose(boolean dispose) {
		this.dispose = dispose;
	}


	public Window getW() {
		return w;
	}


	public void setW(Window w) {
		this.w = w;
	}


	public boolean isOutFlag() {
		return outFlag;
	}


	public void setOutFlag(boolean outFlag) {
		this.outFlag = outFlag;
	}


	public int getFrames() {
		return frames;
	}


	public void setFrames(int frames) {
		this.frames = frames;
	}
	
}
